//
//  CommentCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/14.
//

import UIKit

class CommentCell: UITableViewCell {
    
    lazy var userImage: UIButton = {
        let btn = UIButton(type: .custom)
        btn.imageView?.contentMode = .scaleAspectFill
        btn.wn_cornerWithBorder(12)
        return btn
    }()

    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .black
        return label
    }()
    
    lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .regular, size: 14)
        label.textColor = .init(named: "black_333333")
        label.numberOfLines = 0
        return label
    }()
    
    lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .regular, size: 12)
        label.textColor = .init(named: "gray_999999")
        label.textAlignment = .right
        return label
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(named: "gray_D0D0D0")
        return view
    }()
    
    var model: CommentDetailModel? {
        didSet {
            if let m = model {
                userImage.wn_setupImg(m.avatar, placeHolder: "placeholder_header")
                nameLabel.text = m.userName
                contentLabel.text = m.comment
                timeLabel.text = m.time?.wn_toDate()
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .white
        contentView.addSubview(userImage)
        contentView.addSubview(nameLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(contentLabel)
        contentView.addSubview(lineView)
        userImage.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(12)
            make.width.height.equalTo(24)
        }
        nameLabel.snp.makeConstraints { make in
            make.centerY.equalTo(userImage.snp.centerY)
            make.left.equalTo(userImage.snp.right).offset(6)
            make.width.lessThanOrEqualTo(scaleWidth(220))
        }
        timeLabel.snp.makeConstraints { make in
            make.centerY.equalTo(nameLabel.snp.centerY)
            make.right.equalTo(-14)
        }
        contentLabel.snp.makeConstraints { make in
            make.top.equalTo(nameLabel.snp.bottom).offset(8)
            make.left.equalTo(userImage.snp.left)
            make.right.equalTo(-14)
        }
        lineView.snp.makeConstraints { (make) in
            make.left.equalTo(userImage.snp.left)
            make.bottom.equalTo(0)
            make.right.equalTo(timeLabel.snp.right)
            make.height.equalTo(0.5)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
